package com.sctvcloud.dongqu.ui.adapter.holder;

import android.content.Context;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.ViewUtils;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseCanPlayActivity;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.ui.fragment.GreatestFragment;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;
import com.sctvcloud.dongqu.ui.utils.OnFullClick;
import com.sctvcloud.dongqu.ui.utils.SampleListener;
import com.sctvcloud.dongqu.ui.utils.SkipUtil;
import com.sctvcloud.dongqu.ui.utils.UIUtils;
import com.sctvcloud.dongqu.ui.widget.BasePlayerView;
import com.sctvcloud.dongqu.utils.TimeUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 24小时新闻条目
 * Created by Administrator on 2017/7/5.
 */

public class News24VideoHolder extends BaseAbsHolder<NewsItem> implements View.OnClickListener {


    @BindView(R.id.video_player)
    public BasePlayerView playerView;
    @BindView(R.id.item_news_video_title)
    protected CustomFontTextView item_news_title_video;
    @BindView(R.id.item_news_video_date)
    protected CustomFontTextView item_news_date_video;
    @BindView(R.id.item_news_video_views)
    protected CustomFontTextView item_news_views_video;
    @BindView(R.id.item_video_image)
    protected CustomEXImageView video_no_img;
    @BindView(R.id.item_news_24_video_media)
    protected ImageView video_play;
    private NewsItem temp;
    private Handler handler = new Handler();
    private boolean isCanPlay = false;
    private NewsItemClickListener listener;
    private OnMuteChangeListener muteChangeListener;
    private OnPlayerFullClickListener fullClickListener;
    private boolean isPlaying=false;

    public boolean isPlaying() {
        return isPlaying;
    }

    public void setPlaying(boolean playing) {
        isPlaying = playing;
    }

    public void setFullClickListener(OnPlayerFullClickListener fullClickListener) {
        this.fullClickListener = fullClickListener;
    }

    public void setMuteChangeListener(OnMuteChangeListener muteChangeListener) {
        this.muteChangeListener = muteChangeListener;
    }

    public void setListener(NewsItemClickListener listener) {
        this.listener = listener;
    }
    public void setMute(boolean isMute) {
        playerView.setMute(isMute);

    }
    public News24VideoHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }
    public void setCanPlay(boolean canPlay) {
        isCanPlay = canPlay;
    }

    public boolean isCanPlay() {
        return isCanPlay;
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    public void playVideo() {
        if (null != playerView && isCanPlay) {
            playerView.startPlayLogic();
            playerView.setEnabled(true);
            isPlaying=true;
        }
        video_play.setVisibility(View.GONE);
    }

    public void pauseVideo() {
        if (null != playerView && isCanPlay) {
            isPlaying=false;
            playerView.onVideoPause();
            playerView.setEnabled(false);
            playerView.release();
            playerView.hideBottom();
        }
        video_play.setVisibility(View.VISIBLE);
    }
    public void onResume(){
        JLog.e("Log","News24VideoHolder:   "+"onResume   Status"+playerView.getStatus());
        if (null != playerView && isCanPlay&&playerView.getStatus()==2) {
            isPlaying=true;
//            playerView.getStatus()
            playerView.onVideoResume();
            playerView.setMute(playerView.isMute());
//            playerView.setEnabled(false);
//            playerView.release();
        }
        video_play.setVisibility(View.GONE);
    }
    private boolean isStop=false;
    @Override
    public void setData(NewsItem t) {
        temp = t;
        if(t.getNewsType()==3){
            item_news_title_video.setText(t.getDataTitle());
            item_news_date_video.setText(temp.getNewsSource()==null?"":temp.getNewsSource());
            if(TextUtils.isEmpty(temp.getNewsSource())){
                item_news_date_video.setVisibility(View.GONE);
            }else {
                item_news_date_video.setVisibility(View.VISIBLE);
            }
            item_news_views_video.setText(UIUtils.getReadingNumber(t.getReadCount()));
            if (!TextUtils.isEmpty(t.getNewsMediaUrl())) {
                setCanPlay(true);
                video_no_img.setVisibility(View.GONE);
                playerView.setVisibility(View.VISIBLE);
                if (null != fragment && fragment instanceof GreatestFragment) {
                    playerView.setMute(((GreatestFragment) fragment).isMute);
                }

                playerView.setFullClick(new OnFullClick() {
                    @Override
                    public void onFullClick(StandardGSYVideoPlayer player, boolean isLandscape) {
                if (null != fragment && fragment instanceof GreatestFragment) {
                      ((GreatestFragment) fragment).onFullClicked(player,isLandscape);
                 }
                        if(fullClickListener!=null){
                            fullClickListener.onFullClick(isLandscape);
                        }
                    }
                });
                playerView.setStandardVideoAllCallBack(new SampleListener() {
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                    }
                });
                playerView.setMuteChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (null != muteChangeListener) {
                            muteChangeListener.OnMuteChange(isChecked);
                        }
                    }
                });
                playerView.setOnPlayerStatusListener(new BasePlayerView.OnPlayerStatusChanged() {
                    @Override
                    public void OnPlaying() {

                        video_play.setVisibility(View.GONE);
                    }

                    @Override
                    public void OnError() {

                    }

                    @Override
                    public void OnStop() {

                    }

                    @Override
                    public void OnEnd() {
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (playerView  != null) {
                                   pauseVideo();
                                    video_play.setVisibility(View.VISIBLE);
                                    playerView.hideBottom();
                                }
                            }
                        }, 100);
                    }
                });
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (playerView.getThumbImge() != null  && null!=context) {
                            GlideUtil.getGlideWithLarge169DefNotFit(context, temp.getNewsImage()).into(playerView.getThumbImge());
                        }
                        playerView.setUp(temp.getNewsMediaUrl(), false, "");
//                        playerView.onVideoPause();
                        playerView.hideBottom();

                    }
                }, 200);
            } else {
                setCanPlay(false);
                playerView.setVisibility(View.GONE);
                GlideUtil.getGlideWithLarge169DefNotFit(context, t.getNewsImage()).skipMemoryCache(true).into(video_no_img);
            }

//            if (null != fragment && fragment instanceof GreatestFragment) {
//                ((GreatestFragment) fragment).initPlayer(playerView);
//            }
            video_play.setVisibility(View.VISIBLE);
            video_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                if (null != listener) {
                    video_play.setVisibility(View.GONE);
                    playerView.showBottom();
                    listener.OnItemClick(temp);
                }
                }
            });
            item_news_title_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (temp == null) {
                        Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    ArrayList<Pair<String, String>> pairs = new ArrayList<>();
                    pairs.add(new Pair<>("页面", "首页"));
                    pairs.add(new Pair<>("栏目", "精选"));
                    pairs.add(new Pair<>("位置", "24小时"));
                    ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
                    tempPair.addAll(pairs);
                    tempPair.add(new Pair<>("行为类型", "点击"));

                    if (activity != null) {
                        GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getNewsTitle(), "", 200, tempPair);
                    } else if (fragment != null) {
                        GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getNewsTitle(), "", 200, tempPair);
                    }
                    HashMap<String, String> map = new HashMap<>();
                    for (Pair<String, String> p : pairs) {
                        map.put(p.first, p.second);
                    }
                    SkipUtil.skipWithNewsItem(context, temp, map);
                }
            });
        }

    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
        if (null != playerView && isCanPlay) {
            isPlaying=false;
            playerView.onVideoPause();
            playerView.setEnabled(false);
            playerView.release();
        }
    }

    @Override
    public void onClick(View v) {

    }
    private boolean resuming=false;

    public boolean isResuming() {
        return resuming;
    }




    public void onPrepared() {
//        JLog.e("nnan pause  "+videoView);
        resuming = false;
        if (playerView != null) {
            playerView.onPrepared();
        }
    }


    public interface NewsItemClickListener {
        void OnItemClick(NewsItem item);
    }
    public interface OnMuteChangeListener {
        void OnMuteChange(boolean isMute);
    }
    public interface OnPlayerFullClickListener {
        void onFullClick(boolean isNormal);
    }

    @Override
    public void onDetached() {
        super.onDetached();

    }
}
